package com.test.socket.clientSocket
{
	import com.utils.Alert;
	import com.utils.ButtonModule;
	import com.utils.TextFieldUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import com.test.socket.model.Constant;
	
	public class SocketUI extends Sprite
	{
		public static const instance:SocketUI = new SocketUI();
		private var _msgTxt:TextField;
		private var _inputTxt:TextField;
		private var _userListTxt:TextField;
		private var _sendBtn:ButtonModule;
		private var _clearBtn:ButtonModule;
		private var _stopReceiveMsgBtn:ButtonModule;
		private var btnList:Array = [];
		private var myUserName:String = 'test2';
		public function SocketUI()
		{
			if(instance) throw Error('不能重复实例化！');
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		}
		
		private function onAddToStageHandler(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
			//Alert.register(this,this.stage);
			setupUI();
			initSocket();
		}
		
		private function setupUI():void
		{
			_msgTxt = TextFieldUtil.newTextField(this,'初始化...',{width:stage.stageWidth-105,height:280,background:true,backgroundColor:0,wordWrap:true});
			_msgTxt.height = 280;
			_userListTxt = TextFieldUtil.newTextField(this,'',{x:_msgTxt.x + _msgTxt.width + 5,width:100,height:280,background:true,backgroundColor:0,wordWrap:true});
			_userListTxt.height = 280;
			_inputTxt = TextFieldUtil.newTextField(this,'在此输入',{y:_msgTxt.x + _msgTxt.height + 40,width:stage.stageWidth-5,height:40,type:TextFieldType.INPUT,border:true,borderColor:0xFFFFFF,background:true,backgroundColor:0xAAAAAA,wordWrap:true},'#FFFFFF',14);
			_inputTxt.height = 40;
			_sendBtn = new ButtonModule(null,['发送']);
			_clearBtn = new ButtonModule(null,['清除']);
			_stopReceiveMsgBtn = new ButtonModule(null,['屏蔽用户']);
			_sendBtn.addEventListener(MouseEvent.CLICK,sendMsgHanlder);
			_clearBtn.addEventListener(MouseEvent.CLICK,clearHanlder);
			_stopReceiveMsgBtn.addEventListener(MouseEvent.CLICK,deleteUserHanlder);
			btnList = [_sendBtn,_clearBtn,_stopReceiveMsgBtn];
			for (var i:int = 0; i < btnList.length; i++) 
			{
				btnList[i].y = _inputTxt.y + _inputTxt.height+10;
				btnList[i].x = (btnList[i].width+10)* i;
				this.addChild(btnList[i])
			}
		}
		
		protected function deleteUserHanlder(e:MouseEvent):void
		{
			var deleteName:String = _inputTxt.text;
			if(ClientSocketManager.instance.userList.indexOf(deleteName) >=0 && deleteName!= ''){
				ClientSocketManager.instance.addStopAcceptUser(deleteName);
			}else{
				showAlertTip('用户名不正确！');
			}
		}
		
		private function initSocket():void{
			ClientSocketManager.instance.appendText = appendText;
			ClientSocketManager.instance.showAlertTip = showAlertTip;
			ClientSocketManager.instance.createSocket('127.0.0.1',9999,myUserName);
		}
		
		private function showAlertTip(value:String = ''):void
		{
			//Alert.instance().showYesMsg(value);
		}
		
		private function clearHanlder(e:MouseEvent):void
		{
			_msgTxt.text = '';
		}	
		
		protected function sendMsgHanlder(event:MouseEvent):void
		{
			if(_inputTxt.text == ''){
				showAlertTip('不能法空消息!')
				return;
			}
			ClientSocketManager.instance.sendMsg(Constant.MESSAGE_HEAD,_inputTxt.text);
		}
		
		/**
		 * 添加文本显示
		 * @param value message.body
		 * @param head message.userName
		 * @param showType 0:_msgTxt 1:_userListTxt
		 */		
		private function appendText(value:*,userName:String='',showType:int = 0):void
		{
			if(showType == 0){
				_msgTxt.appendText((userName!=''?'\n':'') + userName + '\n' + value);
				_msgTxt.scrollV = _msgTxt.maxScrollV;//滚动到最下面
			}else if(showType == 1){
				_userListTxt.htmlText= '<font color="#FFCC00">' + value + '</font>';
			}
		}
		
		
		
		
		
		
		
		
	}
}